package com.lws.basic.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lws.basic.R;


/**
 * 标题栏
 * @see #padding(TypedArray) 设置填充，在布局文件设置填充无效
 */

public class TitleLayout extends ConstraintLayout implements View.OnClickListener {


    private OnClickListener mClickListenertLeft;
    private OnClickListener mClickListenerRight;

    public TitleLayout(@NonNull Context context) {
        super(context);
    }

    public TitleLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public TitleLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_title,this);
        //获得资源文件的参数
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TitleLayout);
        //左面图片的点击事件(为了使用户更容易点击,扩大点击范围,点击事件设置到帧布局.
        //默认的左点击事件为关闭当前界面
        mClickListenertLeft = new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) getContext()).finish();
            }
        };

        padding(ta);
        title(ta);
        leftImage(ta);
        leftText(ta);
        rightImage(ta);
        rightText(ta);
        ta.recycle();
    }


    private void title(TypedArray ta) {
        titleText(ta);
        titleDrawable(ta);
    }

    private void titleText(TypedArray ta) {
        TextView tv_title = findViewById(R.id.tv_title_in_title);
        String title = ta.getString(R.styleable.TitleLayout_title_text);
        int color = ta.getColor(R.styleable.TitleLayout_title_text_color,
                getResources().getColor(R.color.black_111111));
        tv_title.setTextColor(color);
        if (title!= null) {
            tv_title.setText(title);
            tv_title.setVisibility(VISIBLE);
        }
    }

    private void titleDrawable(TypedArray ta) {
        Drawable drawable = ta.getDrawable(R.styleable.TitleLayout_title_image);
        if (drawable != null) {
            ImageView imageView = findViewById(R.id.iv_title_int_title);
            imageView.setImageDrawable(drawable);
            imageView.setVisibility(VISIBLE);
        }
    }

    private void leftImage(TypedArray ta) {
        Drawable drawable_left = ta.getDrawable(R.styleable.TitleLayout_left_image);
        if (drawable_left != null) {
            ImageView iv_left = findViewById(R.id.iv_left_in_title);
            iv_left.setVisibility(VISIBLE);
            iv_left.setOnClickListener(this);
            iv_left.setImageDrawable(drawable_left);
        }

    }

    private void leftText(TypedArray ta) {
        String textLeft = ta.getString(R.styleable.TitleLayout_left_text);
        if (textLeft != null) {
            TextView tv_left = findViewById(R.id.tv_left_in_title);
            tv_left.setVisibility(VISIBLE);
            tv_left.setOnClickListener(this);
            tv_left.setText(textLeft);

            Drawable drawable = ta.getDrawable(R.styleable.TitleLayout_left_text_image);
            if (drawable!= null) {
                tv_left.setCompoundDrawablesWithIntrinsicBounds(null, drawable , null, null);
            }
        }

    }

    private void rightText(TypedArray ta) {
        String textRight = ta.getString(R.styleable.TitleLayout_right_text);
        if (textRight != null) {
            TextView tv_right = findViewById(R.id.tv_right_in_title);
            int currentTextColor = tv_right.getCurrentTextColor();
            int color = ta.getColor(R.styleable.TitleLayout_right_text_color, currentTextColor);
            tv_right.setVisibility(VISIBLE);
            tv_right.setOnClickListener(this);
            tv_right.setText(textRight);
            tv_right.setTextColor(color);
        }
    }

    private void rightImage(TypedArray ta) {
        Drawable drawable_right = ta.getDrawable(R.styleable.TitleLayout_right_image);
        if (drawable_right != null) {
            ImageView iv_right = findViewById(R.id.iv_right_in_title);
            iv_right.setVisibility(VISIBLE);
            iv_right.setOnClickListener(this);
            iv_right.setImageDrawable(drawable_right);
        }
    }

    /**
     * 设置填充
     * @param ta
     */
    private void padding(TypedArray ta) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return;
        int height = 0;
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取相应的尺寸值
            height = getResources().getDimensionPixelSize(resourceId);
        }
        boolean fitTransport = ta.getBoolean(R.styleable.TitleLayout_fitTransport, true); //是否适配沉浸式状态栏
        if (!fitTransport) height = 0;
        setPadding(getPaddingLeft(),getPaddingTop() + height,getPaddingRight(),getPaddingBottom());
//        setPadding(0,0,0,0);
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.iv_left_in_title) {
            if (mClickListenertLeft != null)
                mClickListenertLeft.onClick(v);
        } else if (i == R.id.tv_left_in_title) {
            if (mClickListenertLeft != null)
                mClickListenertLeft.onClick(v);
        } else if (i == R.id.iv_right_in_title) {
            if (mClickListenerRight != null)
                mClickListenerRight.onClick(v);
        } else if (i == R.id.tv_right_in_title) {
            if (mClickListenerRight != null)
                mClickListenerRight.onClick(v);

        }
    }

    /**
     * @param title 设置标题
     */
    public void setText(String title) {
        TextView tv_title = findViewById(R.id.tv_title_in_title);
        tv_title.setText(title);
    }

    public void setRightText(String rightText) {
        TextView textView = findViewById(R.id.tv_right_in_title);
        textView.setText(rightText);
    }

    /**
     * @param clickListenertLeft  左步点击事件
     */
    public void setClickListenertLeft(OnClickListener clickListenertLeft) {
        mClickListenertLeft = clickListenertLeft;
    }

    /**
     * @param clickListenerRight 右部点击事件
     */
    public void setClickListenerRight(OnClickListener clickListenerRight) {
        mClickListenerRight = clickListenerRight;
    }


}
